/*jslint adsafe:false, 
 bitwise: true, 
 browser:true, 
 cap:false, 
 debug:false,
 eqeqeq: true,
 evil: false,
 forin: false,
 fragment:false,
 laxbreak:false, 
 nomen:true, 
 on:false,
 passfail:false, 
 plusplus:true, 
 rhino:false, 
 undef:true, 
 white:true, 
 widget:false */

/*global localizedStrings*/
(function (localizedStrings)
{
  var counterText = document.getElementById("counter").firstChild;
  if (window.widget)
  {
    //counterText.parentNode.style.width = screen.availWidth + "px";
    window.resizeTo(screen.availWidth, counterText.parentNode.offsetHeight);
  }
  
  var counter = document.getElementById("counter");
  var back = document.getElementById("back");
  var info = document.getElementById("info");
  var done = document.getElementById("done");
  var credit = document.getElementById("credit");
  
  var isFront = true;
  var startTime;
  var updateTimeout;
  
  function stopTimeout()
  {
    if (updateTimeout)
    {
      updateTimeout = clearTimeout(updateTimeout);
    }
  }
  
  function updateCounter()
  {
    try
    {
      var nextValue = "" + (new Date().getTime() - startTime) % 10000;
      if (nextValue < 10)
      {
        nextValue = "0:00" + "" + nextValue;
      }
      else if (nextValue < 100)
      {
        nextValue = "0:0" + "" + nextValue;
      }
      else if (nextValue < 1000)
      {
        nextValue = "0:" + "" + nextValue;
      }
      else
      {
        nextValue = nextValue[0] + ":" + "" + nextValue[1] + "" + nextValue[2] + "" + nextValue[3];
      }
      counterText.nodeValue = nextValue;
      updateTimeout = setTimeout(updateCounter, 1);
    }
    catch (error)
    {
      stopTimeout();
      alert(error);
    }
  }
  
  function stopStart(event)
  {
    try
    {
      if (updateTimeout)
      {
        stopTimeout();
      }
      else
      {
        startTime = new Date().getTime();
        updateCounter();
      }
      if (event)
      {
        event.stopPropagation();
        event.preventDefault();
      }
    }
    catch (error)
    {
      stopTimeout();
      alert(error);
    }
  }
  
  function flip(event)
  {
    try
    {
      if (isFront)
      {
        if (window.widget) 
        {
          window.widget.prepareForTransition("ToBack");
        }
        counter.style.visibility = "hidden";
        info.style.display = "none";
        back.style.display = "block";
        credit.style.visibility = "visible";
        stopTimeout();
        isFront = false;
      }
      else
      {
        if (window.widget) 
        {
          window.widget.prepareForTransition("ToFront");
        }
        counter.style.visibility = "visible";
        info.style.display = "block";
        back.style.display = "none";
        credit.style.visibility = "hidden";
        stopStart();
        isFront = true;
      }
      if (event)
      {
        event.stopPropagation();
        event.preventDefault();
      }
      
      if (window.widget)
      {
        setTimeout(function () 
        {
          window.widget.performTransition();
        }, 100);
      }
    }
    catch (error)
    {
      stopTimeout();
      alert("flip: " + error);
    }
  }
  
  document.getElementsByTagName("body")[0].addEventListener("keydown", 
  function ()
  {
    if (isFront)
    {
      stopStart();
    }
  }, true);
  document.getElementById("info").addEventListener("click", flip, true);
  if (localizedStrings)
  {
    while (done.firstChild)
    {
      done.removeChild(done.firstChild);
    }
    done.appendChild(document.createTextNode(localizedStrings.Done));
    while (credit.firstChild)
    {
      credit.removeChild(credit.firstChild);
    }
    credit.appendChild(document.createTextNode(localizedStrings.credit));
  }
  done.addEventListener("click", flip, true);
  startTime = new Date().getTime();
  if (window.widget)
  {
    document.getElementById("homepage").addEventListener("click", function () 
    {
      window.widget.openURL("http://inputlag.googlecode.com");
    }, false);
    window.widget.onhide = stopTimeout;
    setTimeout(function () 
    {
      window.widget.onshow = function ()
      {
        if (isFront)
        {
          stopStart();
        }
      };
    }, 300);
  }
  updateCounter();
})(localizedStrings);